package com.ms.seguridad;

import com.ms.seguridad.bean.ModuleBean;
import com.ms.services.commons.schema.AuditInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.List;

/**
 * User: gmc
 * Date: 31/01/14
 */
public class UserInfo extends User {
    protected final Logger LOG = LoggerFactory.getLogger(getClass());


    protected List<ModuleBean> modulesType;

    protected String ipAddress;


    public UserInfo(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }

    public UserInfo(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    }

/*
    public UserInfo(String username, String password,String ipAddress, Collection<? extends GrantedAuthority> authorities, UserType userType, RoleType roleType, ApplicationType applicationType, List<ModuleBean> modulesType) {
        super(username, password, authorities);
        this.ipAddress = ipAddress;
        this.userType = userType;
        this.roleType = roleType;
        this.applicationType = applicationType;
        this.modulesType = modulesType;
    }
*/

/*
    public UserType getUserType() {
        return userType;
    }

    public RoleType getRoleType() {
        return roleType;
    }

    public ApplicationType getApplicationType() {
        return applicationType;
    }
*/

    public List<ModuleBean> getModulesType() {
        return modulesType;
    }

    public String getIpAddress() {
        return ipAddress;
    }
/*

    public String getFullName(){
        return getUserType().getUserLogin()+ " "+ getUserType().getUserLastName() + ","+getUserType().getUserFirstName();
    }
*/

    public AuditInfo getAuditInfo() {
        AuditInfo auditInfo = new AuditInfo();
        auditInfo.setUserLogin(getUsername());
//        auditInfo.setUserFirstName(getUserType().getUserFirstName());
//        auditInfo.setUserLastName(getUserType().getUserLastName());
        auditInfo.setUserIpAddress(getIpAddress());
        auditInfo.setUserHostName(getHostNameFor(getIpAddress()));
        return auditInfo;
    }

    private String getHostNameFor(String ipAddress){
        String hostName = null;
        try {
            InetAddress inetAddress = InetAddress.getByName(ipAddress);
            hostName = inetAddress.getHostName();
        } catch (UnknownHostException e) {
            LOG.error("No se puede determinar el hostName del ip:"+ipAddress,e);
        }
        return hostName;
    }

}


